<template>
  <div class="loginpage">
    <el-form
      ref="loginform"
      :model="form"
      :rules="rules"
      label-width="60px"
      label-position="left"
    >
      <el-form-item prop="loginName">
        <el-input
          prefix-icon="el-icon-mobile-phone"
          v-model="form.loginName"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          v-model="form.password"
          type="password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input
          prefix-icon="el-icon-circle-check"
          v-model.lazy="form.code"
          placeholder="请输入验证码"
            @keyup.enter.native="handleLogin"
        ></el-input>
      </el-form-item>
      <el-button
        type="primary"
        :loading="loading"
        class="login-btn"
        @click.native.prevent="onSubmit"
        >登录</el-button
      >
      <div class="logo">
        <img
          src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
          alt=""
        />
      </div>
      <div class="keycode" @click="getrandom">
        <img
          :src="`https://likede2-java.itheima.net/api/user-service/user/imageCode/${form.clientToken}`"
          alt=""
        />
      </div>
    </el-form>
  </div>
</template>

<script>
// import { Message } from 'element-ui'
export default {
  name: 'login-page',
  data () {
    return {
      form: {
        loginName: 'admin', // 用户名
        password: 'admin', // 密码
        code: '', // 验证码
        clientToken: '1', // 随机数
        mobile: '',
        loginType: 0,
        account: ''
      },
      rules: {
        // 必填
        loginName: [
          {
            required: true,
            message: '请输入账号',
            trigger: ['blur', 'change']
          },
          {
            pattern: /^[a-zA-Z]{5,12}$/,
            message: '账号为非空&&英文并且长度位5-12'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          },
          {
            pattern: /^[a-zA-Z]{5,12}$/,
            message: '账号为非空&&英文并且长度位5-12'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入验证码',
            trigger: ['blur', 'change']
          },
          { pattern: /[\u4e00-\u9fa5_a-zA-Z0-9_]{4}/, message: '验证码为4位' }
        ]

      },
      loading: false
    }
  },
  methods: {
    getrandom () {
      // 生成一个随机数
      this.form.clientToken = Math.random()
    },
    async onSubmit () {
      this.loading = true
      try {
        // console.log(this.form)
        // 先等一等
        await this.$refs.loginform.validate()
        await this.$store.dispatch('login/logInFn', this.form)
      } catch (err) {
        // console.log(err)
      } finally {
        this.loading = false
        // this.$nextTick(() => {
        this.getrandom()
        this.$refs.loginform.resetFields()
        // })
      }

    //   this.$refs.loginform.validate().then(
    //     () => {
    //
    //       console.log(this.form)
    //       //* dom更新后在执行
    //       this.$nextTick(() => {
    //         this.$refs.loginform.resetFields()
    //         this.getrandom()
    //         this.loading = false
    //       })
    //     },
    //     (err) => {
    //       console.log(err)
    //     }
    //   )
    },
    handleLogin () {
      // console.log(1)
      this.onSubmit()
    }

  }
}
</script>
<style lang="scss" scoped>
.loginpage {
  min-height: 100vh;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: url("http://likede2-admin.itheima.net/img/background.be4fae7d.png")
    no-repeat center / cover;
  .el-form {
    position: relative;
    width: 530px;
    height: 296px;
    padding-top: 70px;
    background: url('https://tse1-mm.cn.bing.net/th/id/OIP-C.nyYvmehQrwu3AC6Fk5DvFAHaMW?w=182&h=304&c=7&r=0&o=5&dpr=1.25&pid=1.7')  repeat 100% ;
    border-radius: 10px;
    ::v-deep .el-input__inner {
      height: 52px;
      width: 420px;
      font-size: 14px;
      line-height: 40px;
    }
    .login-btn {
      height: 52px;
      width: 530px;
      // border-radius: 50% ;
      color: rgb(255, 255, 255);
      border-radius: 5px;
      margin-left: 60px;
      transform: translate(-59px);
      // background-color: #6377ed;
      background: url('https://tse1-mm.cn.bing.net/th/id/OIP-C.nyYvmehQrwu3AC6Fk5DvFAHaMW?w=182&h=304&c=7&r=0&o=5&dpr=1.25&pid=1.7')  repeat 100% ;
    }
  }
  .logo {
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    overflow: hidden;
    border-radius: 50%;
    margin-left: -48px;
    img {
      width: 100%;
    }
  }
  .keycode {
    position: absolute;
    width: 130px;
    height: 50px;
    bottom: 96px;
    right: 50px;
  }
}
</style>
